import { RESPONSE_CODE, STORE_KEY, UPLOAD_VIDEO_SIZE } from "@/common/config";
import { handleCommonUpload, handleLoginCallback, showToast } from "@/common/util";
import Taro from "@tarojs/taro";
import { useEffect, useState } from "react";



const useUpladVideoAction = () => {
    const [videoPoster, setVideoPoster] = useState('');
    const [hasEditVideo, setHasEditVideo] = useState(false)

    const [videoUrl, setVideoUrl] = useState('');
    const [playStatus, setPlayStatus] = useState(false);

    const handleBeforeUploadVideo = (event) => {
        const { file, callback } = event.detail
        if (file.size > UPLOAD_VIDEO_SIZE) {
            showToast('最大只能上传100M的视频')

            callback(false)
        } else {
            callback(true)
        }
    }
    const handleAfterUploadVideo = async (event) => {
        const { file } = event.detail
        console.log(file)
        let uploadVideoUrlRes = await handleCommonUpload(file.url);
        if (uploadVideoUrlRes.code === RESPONSE_CODE.LOGIN_EXPIRE || uploadVideoUrlRes.code === RESPONSE_CODE.LOGIN_NEED) {
            handleLoginCallback(async () => {
                uploadVideoUrlRes = await handleCommonUpload(file.url);
            })
            // Taro.removeStorageSync(STORE_KEY.PHONE)
            // await handleInitLogin().then(async () => {
            //     uploadVideoUrlRes = await handleCommonUpload(file.url);
            // })
        }
        if (uploadVideoUrlRes.code === RESPONSE_CODE.SUCCESS) {

            const videoUrl = uploadVideoUrlRes.fileUrl
            setVideoUrl(videoUrl)
            setHasEditVideo(true)
            Taro.setStorageSync(STORE_KEY.VIDEO_URL, videoUrl)
        }

        return
        Taro.getVideoInfo({
            src: file.url,
            success:async (videoInfo) => {
                console.log(videoInfo)
                if (videoInfo.size > 100000) {
                    Taro.showModal({
                        content: '视频最大为100M',
                        success(res) {
                            if (res.confirm) {
                                console.log('用户点击确定')
                                // that.btn_mediaType()
                            } else if (res.cancel) {
                                console.log('用户点击取消')
                            }
                        }
                    })
                    return

                }
                const canvasWidth = videoInfo.width;
                const canvasHeight = videoInfo.height;
                let resolution
                let bitrate
                const maxWidth = 1280;
                const minWidth = 576;

                var targetWidth = canvasWidth
                var targetHeight = canvasHeight;
                if (canvasHeight > canvasWidth && canvasWidth > minWidth) {  // 高视频
                    targetHeight = Math.round(minWidth * (canvasHeight / canvasWidth))
                    targetWidth = minWidth
                }
                if (canvasWidth > canvasHeight && canvasWidth > maxWidth) { // 宽视频
                    targetWidth = canvasWidth
                    targetHeight = canvasHeight;

                    targetWidth = maxWidth
                    targetHeight = Math.round(maxWidth * (canvasHeight / canvasWidth))
                }
                if(videoInfo.size<5000){
                    let uploadVideoUrlRes = await handleCommonUpload(file.url);
                    if (uploadVideoUrlRes.code === RESPONSE_CODE.LOGIN_EXPIRE || uploadVideoUrlRes.code === RESPONSE_CODE.LOGIN_NEED) {
                        handleLoginCallback(async () => {
                            uploadVideoUrlRes = await handleCommonUpload(file.url);
                        })
                        // Taro.removeStorageSync(STORE_KEY.PHONE)
                        // await handleInitLogin().then(async () => {
                        //     uploadVideoUrlRes = await handleCommonUpload(file.url);
                        // })
                    }
                    if (uploadVideoUrlRes.code === RESPONSE_CODE.SUCCESS) {
            
                        const videoUrl = uploadVideoUrlRes.fileUrl
                        setVideoUrl(videoUrl)
                        setHasEditVideo(true)
                        Taro.setStorageSync(STORE_KEY.VIDEO_URL, videoUrl)
                    }
                    return
                }
                
                if( targetHeight >= 720 && videoInfo.bitrate >= 1500 && canvasHeight > canvasWidth){
                    bitrate = 1400
                    resolution = (targetHeight / videoInfo.height).toFixed(2)
                } else if( targetHeight >= 720 && videoInfo.bitrate >= 1500){
                    bitrate = 1300
                    resolution = (targetHeight / videoInfo.height).toFixed(2)
                } else if( targetHeight >= 720 && videoInfo.bitrate < 1500 ){
                    bitrate = 900
                    resolution = (targetHeight / videoInfo.height).toFixed(2)
                } else if( targetHeight < 720 && videoInfo.bitrate >= 900){
                    bitrate = 700
                    resolution = 1
                } else if( targetHeight < 720 && videoInfo.bitrate < 900){
                    bitrate = videoInfo.bitrate
                    resolution = 1
                } 
                //@ts-ignore
                Taro.compressVideo({
                    src:file.url,
                    bitrate:bitrate,
                    fps: 30,
                    resolution,
                    success:(resp)=>{
                        Taro.hideLoading()
                        console.log(resp,'------压缩成功')
                    },fail:(err)=>{
                        Taro.hideLoading()
                    }
                })


            }
        })
        // return
       
        // Taro.compressVideo({
        //     src: file.url,
        //     quality: 'high',
        //     bitrate: 1032,
        //     fps: 24,
        //     resolution:0.5,
        //    async success (res) {
        //     //   console.log(res)
        //     //   showToast('压缩成功')
        //     let uploadVideoUrlRes = await handleCommonUpload(res.tempFilePath);
        //     if (uploadVideoUrlRes.code === RESPONSE_CODE.LOGIN_EXPIRE || uploadVideoUrlRes.code=== RESPONSE_CODE.LOGIN_NEED) {
        //         handleLoginCallback(async()=>{
        //             uploadVideoUrlRes = await handleCommonUpload(res.tempFilePath);
        //         })
        //         // Taro.removeStorageSync(STORE_KEY.PHONE)
        //         // await handleInitLogin().then(async () => {
        //         //     uploadVideoUrlRes = await handleCommonUpload(file.url);
        //         // })
        //     }
        //     if (uploadVideoUrlRes.code === RESPONSE_CODE.SUCCESS) {

        //         const videoUrl = uploadVideoUrlRes.fileUrl
        //         setVideoUrl(videoUrl)
        //         setHasEditVideo(true)
        //         Taro.setStorageSync(STORE_KEY.VIDEO_URL, videoUrl)
        //     }

        //     },
        //     fail (err) {
        //     //   console.log("压缩失败")
        //       showToast('压缩失败')
        //     }
        //   })





    }

    useEffect(() => {
        if (playStatus) {
            const videoContext = Taro.createVideoContext('video')
            videoContext.play()
        } else {
            const videoContext = Taro.createVideoContext('video')
            videoContext.pause()
        }
    }, [playStatus])
    const handleDeleteVideo = () => {
        Taro.removeStorageSync(STORE_KEY.VIDEO_URL);
        setVideoUrl('')
        setHasEditVideo(true)
        setPlayStatus(false)
        setVideoPoster('')
    }
    return {
        videoPoster,
        videoUrl,
        handleDeleteVideo,
        handleAfterUploadVideo,
        handleBeforeUploadVideo,
        setVideoUrl,
        playStatus, setPlayStatus,
        hasEditVideo
    }

}

export default useUpladVideoAction;